﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;

namespace WIC_Data_Manager
{
    public partial class fSplash : Form
    {
        public fSplash()
        {
            InitializeComponent();
        }

        public void ClearFields()
        {
            tbPassword.Clear();
            tbUsername.Clear();
        }

        public bool PingTest()
        {
            Ping ping = new Ping();

            PingReply pingStatus = ping.Send(IPAddress.Parse(Globals.ServerIPAddress));

            if (pingStatus.Status == IPStatus.Success)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void fSplash_Load(object sender, EventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                MessageBox.Show("Internet connection could not be established.\nPlease check to make sure you are connected to the Internet.", "WIC Data Manager: Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }

        private void bLogin_Click(object sender, EventArgs e)
        {
            if (cbLocalServer.Checked)
            {
                Globals.ServerIPAddress = "192.168.2.75";
            }
            else
            {
                Globals.ServerIPAddress = "76.18.250.63";
            }
            Globals.DataBases.Initiation = new SQLDataBase(Globals.ServerIPAddress, "WICInitiation", Globals.DataBaseUserName, Globals.DataBasePassword);
            Globals.DataBases.Participation = new SQLDataBase(Globals.ServerIPAddress, "WICParticipation", Globals.DataBaseUserName, Globals.DataBasePassword);
            Globals.DataBases.Users = new SQLDataBase(Globals.ServerIPAddress, "WICUsers", Globals.DataBaseUserName, Globals.DataBasePassword);
            if (!PingTest())
            {
                MessageBox.Show("Cannot access server.\nPlease check your Internet connection and/or Firewall settings.", "WIC Data Manager: Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string username = tbUsername.Text.ToString();
                string userLoginAttempt = "";
                string password = tbPassword.Text.ToString();
                string passwordLoginAttempt = "";
                string userPermission;
                Globals.DataBases.Users.Connect();

                //Login Attempt
                userLoginAttempt = Globals.DataBases.Users.SimpleQuery("SELECT * FROM Users WHERE Username='" + username + "'");
                passwordLoginAttempt = Globals.DataBases.Users.SimpleQuery("SELECT Password FROM Users WHERE Username='" + username + "'");

                if (userLoginAttempt.Equals(""))
                {
                    MessageBox.Show("The username you entered does not match any records.\nPlease try again");
                }
                else if (password != passwordLoginAttempt)
                {
                    MessageBox.Show("Invalid password for Username: " + username);
                }
                else
                {
                    userPermission = Globals.DataBases.Users.SelectFromWhereQuery("Permission", "Users", "Username='" + username + "'");
                    //MessageBox.Show(username + " has logged in successfully.");
                    if (userPermission == "Write")
                    {
                        Globals.CurrentlyLoggedIn = username;
                        Globals.Forms.ManagerScreen = new fManager(true);
                    }
                    else
                    {
                        Globals.CurrentlyLoggedIn = username;
                        Globals.Forms.ManagerScreen = new fManager(false);
                    }
                    Globals.Forms.ManagerScreen.Show();
                    this.Hide();
                }
            }
        }
    }
}

